package cn.itcast.service.impl;

import cn.itcast.dao.CheckGroupDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.CheckItem;
import cn.itcast.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @version V1.0
 * @author: luoqianjin
 * @date: 2019/7/27 18:47
 * @description:
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    //添加检查组合，同时需要设置检查组合和检查项的关联关系
    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        //新增基本信息
        checkGroupDao.add(checkGroup);
        //获取新增检查组Id
        Integer id = checkGroup.getId();
        //新增关联信息
        List<Integer> checkItemidList = Arrays.asList(checkitemIds);
        if (!CollectionUtils.isEmpty(checkItemidList)) {
           /* for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroup_id", id);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.addRelationBetweenCheckGroupAndCheckItem(map);*/
           checkItemidList.forEach(checkitemId->{
               checkGroupDao.addRelationBetweenCheckGroupAndCheckItem(id, checkitemId);
           });
            }
        }

    @Override
    public PageResult findpage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
       Page<CheckItem> page = checkGroupDao.findpage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Long id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
    }

    @Override
    public List<CheckGroup> findall() {
        return checkGroupDao.findall();
    }

    @Override
    public void delete(Integer id) {
        checkGroupDao.deleteCheckgroupAndCheckitem(id);
        checkGroupDao.deleteCheckgroupById(id);
    }

}
